package cn.wolfcode.crm.service.impl;


import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.AttendanceMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AttendanceServiceImpl implements IAttendanceService {

	@Autowired
	private AttendanceMapper attendanceMapper;

    public PageResult query(QueryObject qo) {
		int count = attendanceMapper.queryForCount(qo);
		if (count == 0) {
			return new PageResult(count, Collections.EMPTY_LIST);
		}
		List<Attendance> result = attendanceMapper.queryForList(qo);
		return new PageResult(count, result);
	}

    /**
     * 签到
     */
    public void signIn() {
        //获取当前登录用户
        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();

        Attendance attendance = new Attendance();
        attendance.setEmp(emp); //当前登录员工
        attendance.setDate(new Date());  //签到日期
        attendance.setClockIn(new Date());  //签到时间
        attendance.setState(true); //考勤状态
        attendanceMapper.insert(attendance);
    }

    /**
     * 签退
     */
    public void signOut() {
        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();
        attendanceMapper.updateSignOut(emp.getId(),new Date());
    }

}
